/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.pda.FileBrowser;

/**
 * @author mamat-dev
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command backCommand1;
    private Command backCommand2;
    private Command okCommand;
    private Command backCommand;
    private Command okCommand1;
    private Command exitCommand;
    private Command exitCommand1;
    private Command okCommand2;
    private Command backCommand3;
    private Command backCommand4;
    private Form SendText;
    private TextField tFSend;
    private FileBrowser FileBrowser;
    private Form Home;
    private StringItem stringItem;
    private Form FileSeleted;
    private TextField textField;
    private Form form;
    private StringItem sIStatus;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getHome());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == FileBrowser) {//GEN-BEGIN:|7-commandAction|1|31-preAction
            if (command == FileBrowser.SELECT_FILE_COMMAND) {//GEN-END:|7-commandAction|1|31-preAction
                // write pre-action user code here
                switchDisplayable(null, getFileSeleted());//GEN-LINE:|7-commandAction|2|31-postAction
            // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|3|34-preAction
                // write pre-action user code here
                switchDisplayable(null, getHome());//GEN-LINE:|7-commandAction|4|34-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|41-preAction
        } else if (displayable == FileSeleted) {
            if (command == backCommand1) {//GEN-END:|7-commandAction|5|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|6|41-postAction
            // write post-action user code here
            } else if (command == backCommand2) {//GEN-LINE:|7-commandAction|7|44-preAction
                // write pre-action user code here
                switchDisplayable(null, getHome());//GEN-LINE:|7-commandAction|8|44-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|19-preAction
        } else if (displayable == Home) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|9|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|10|19-postAction
            // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|11|23-preAction
                // write pre-action user code here
                switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|12|23-postAction
            // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|13|48-preAction
                // write pre-action user code here
                new Thread(new Runnable() {

                    public void run() {
                        tFSend.setLabel("");
                        tFSend.setString("");
                    }
                }).start();
                switchDisplayable(null, getSendText());//GEN-LINE:|7-commandAction|14|48-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|55-preAction
        } else if (displayable == SendText) {
            if (command == backCommand3) {//GEN-END:|7-commandAction|15|55-preAction
                // write pre-action user code here
                switchDisplayable(null, getHome());//GEN-LINE:|7-commandAction|16|55-postAction
            // write post-action user code here
            } else if (command == okCommand2) {//GEN-LINE:|7-commandAction|17|53-preAction
                // write pre-action user code here
                new Thread(new Runnable() {

                    public void run() {
                        HttpConnection hc = null;
                        InputStream in = null;
                        OutputStream out = null;

                        try {
                            String message = "message="+tFSend.getString();
                            String url = "http://localhost:8080/WebJ2MEConnection/Save";
                            hc = (HttpConnection) Connector.open(url);
                            hc.setRequestMethod(HttpConnection.POST);
                            hc.setRequestProperty("Content-Type",
                                    "application/x-www-form-urlencoded");
                            hc.setRequestProperty("Content-Length",
                                    Integer.toString(message.length()));
                            out = hc.openOutputStream();
                            out.write(message.getBytes());
                            in = hc.openInputStream();
                            int length = (int) hc.getLength();
                            byte[] data = new byte[length];
                            in.read(data);
                            String response = new String(data);
                            sIStatus.setLabel("Success Send");
                            sIStatus.setText(response);

                        } catch (IOException e) {
                            sIStatus.setLabel("Error Send");
                            sIStatus.setText("Connection failed");
                        } finally {
                            try {
                                if (out != null) {
                                    out.close();
                                }
                                if (in != null) {
                                    in.close();
                                }
                                if (hc != null) {
                                    hc.close();
                                }
                            } catch (IOException ioe) {
                            }

                        }
                    }
                }).start();
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|18|53-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|61-preAction
        } else if (displayable == form) {
            if (command == backCommand4) {//GEN-END:|7-commandAction|19|61-preAction
                // write pre-action user code here
                switchDisplayable(null, getHome());//GEN-LINE:|7-commandAction|20|61-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|7-postCommandAction
        }//GEN-END:|7-commandAction|21|7-postCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|22|
    //</editor-fold>//GEN-END:|7-commandAction|22|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
        // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Home ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of Home component.
     * @return the initialized component instance
     */
    public Form getHome() {
        if (Home == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            Home = new Form("J2ME and Server Communication using POST", new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            Home.addCommand(getExitCommand());
            Home.addCommand(getOkCommand());
            Home.addCommand(getOkCommand1());
            Home.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
        // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return Home;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            stringItem = new StringItem(" Server Communication", "This application is example for simple communication in J2ME and Server");//GEN-LINE:|16-getter|1|16-postInit
        // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            okCommand = new Command("File Browser", Command.OK, 0);//GEN-LINE:|22-getter|1|22-postInit
        // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|33-getter|1|33-postInit
        // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: FileBrowser ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of FileBrowser component.
     * @return the initialized component instance
     */
    public FileBrowser getFileBrowser() {
        if (FileBrowser == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            FileBrowser = new FileBrowser(getDisplay());//GEN-BEGIN:|29-getter|1|29-postInit
            FileBrowser.setTitle("File Browser");
            FileBrowser.setCommandListener(this);
            FileBrowser.addCommand(FileBrowser.SELECT_FILE_COMMAND);
            FileBrowser.addCommand(getBackCommand());//GEN-END:|29-getter|1|29-postInit
        // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return FileBrowser;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|38-getter|1|38-postInit
        // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|38-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            backCommand1 = new Command("File Browser", Command.BACK, 0);//GEN-LINE:|40-getter|1|40-postInit
        // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of backCommand2 component.
     * @return the initialized component instance
     */
    public Command getBackCommand2() {
        if (backCommand2 == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            backCommand2 = new Command("Home", Command.BACK, 0);//GEN-LINE:|43-getter|1|43-postInit
        // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return backCommand2;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            okCommand1 = new Command("Send Text", Command.OK, 0);//GEN-LINE:|47-getter|1|47-postInit
        // write post-init user code here
        }//GEN-BEGIN:|47-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|47-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: FileSeleted ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of FileSeleted component.
     * @return the initialized component instance
     */
    public Form getFileSeleted() {
        if (FileSeleted == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            FileSeleted = new Form("form1", new Item[] { getTextField() });//GEN-BEGIN:|37-getter|1|37-postInit
            FileSeleted.addCommand(getBackCommand1());
            FileSeleted.addCommand(getBackCommand2());
            FileSeleted.setCommandListener(this);//GEN-END:|37-getter|1|37-postInit
        // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return FileSeleted;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of textField component.
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            textField = new TextField("textField", null, 32, TextField.ANY);//GEN-LINE:|46-getter|1|46-postInit
        // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return textField;
    }
    //</editor-fold>//GEN-END:|46-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SendText ">//GEN-BEGIN:|49-getter|0|49-preInit
    /**
     * Returns an initiliazed instance of SendText component.
     * @return the initialized component instance
     */
    public Form getSendText() {
        if (SendText == null) {//GEN-END:|49-getter|0|49-preInit
            // write pre-init user code here
            SendText = new Form("SendText", new Item[] { getTFSend() });//GEN-BEGIN:|49-getter|1|49-postInit
            SendText.addCommand(getOkCommand2());
            SendText.addCommand(getBackCommand3());
            SendText.setCommandListener(this);//GEN-END:|49-getter|1|49-postInit
        // write post-init user code here
        }//GEN-BEGIN:|49-getter|2|
        return SendText;
    }
    //</editor-fold>//GEN-END:|49-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of okCommand2 component.
     * @return the initialized component instance
     */
    public Command getOkCommand2() {
        if (okCommand2 == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            okCommand2 = new Command("Send Text", Command.OK, 0);//GEN-LINE:|52-getter|1|52-postInit
        // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return okCommand2;
    }
    //</editor-fold>//GEN-END:|52-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand3 ">//GEN-BEGIN:|54-getter|0|54-preInit
    /**
     * Returns an initiliazed instance of backCommand3 component.
     * @return the initialized component instance
     */
    public Command getBackCommand3() {
        if (backCommand3 == null) {//GEN-END:|54-getter|0|54-preInit
            // write pre-init user code here
            backCommand3 = new Command("Back", Command.BACK, 0);//GEN-LINE:|54-getter|1|54-postInit
        // write post-init user code here
        }//GEN-BEGIN:|54-getter|2|
        return backCommand3;
    }
    //</editor-fold>//GEN-END:|54-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tFSend ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of tFSend component.
     * @return the initialized component instance
     */
    public TextField getTFSend() {
        if (tFSend == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            tFSend = new TextField("Create your message", null, 32, TextField.ANY);//GEN-LINE:|51-getter|1|51-postInit
        // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return tFSend;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            form = new Form("Send Status", new Item[] { getSIStatus() });//GEN-BEGIN:|57-getter|1|57-postInit
            form.addCommand(getBackCommand4());
            form.setCommandListener(this);//GEN-END:|57-getter|1|57-postInit
        // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return form;
    }
    //</editor-fold>//GEN-END:|57-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: sIStatus ">//GEN-BEGIN:|59-getter|0|59-preInit
    /**
     * Returns an initiliazed instance of sIStatus component.
     * @return the initialized component instance
     */
    public StringItem getSIStatus() {
        if (sIStatus == null) {//GEN-END:|59-getter|0|59-preInit
            // write pre-init user code here
            sIStatus = new StringItem(" ", null);//GEN-LINE:|59-getter|1|59-postInit
        // write post-init user code here
        }//GEN-BEGIN:|59-getter|2|
        return sIStatus;
    }
    //</editor-fold>//GEN-END:|59-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand4 ">//GEN-BEGIN:|60-getter|0|60-preInit
    /**
     * Returns an initiliazed instance of backCommand4 component.
     * @return the initialized component instance
     */
    public Command getBackCommand4() {
        if (backCommand4 == null) {//GEN-END:|60-getter|0|60-preInit
            // write pre-init user code here
            backCommand4 = new Command("Home", Command.BACK, 0);//GEN-LINE:|60-getter|1|60-postInit
        // write post-init user code here
        }//GEN-BEGIN:|60-getter|2|
        return backCommand4;
    }
    //</editor-fold>//GEN-END:|60-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
